package com.person.product.action;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.Date;

import com.person.base.model.BaseUser;
import com.person.commons.struts2.BaseAction;
import com.person.commons.utils.Constants;
import com.person.commons.utils.FlipPageInfo;
import com.person.product.model.Product;
import com.person.product.service.ProductService;
import com.person.title.model.TitleFunc;

public class ProductAction extends BaseAction{
	
	private ProductService productService;
	private Product product;
	private String productPhoto;
	private String productPhotoFileName;
	private FlipPageInfo flipData =new FlipPageInfo();
	private String productName;
	private String productIntro;
	private String productId;
	private TitleFunc titleFunc;
	
	public String index(){
		return "create";
	}
	/**
	 * 创建产品对象
	 * @return
	 */
	public String create(){
		// 获得登录用户的身份
		BaseUser user = (BaseUser) request.getSession().getAttribute(
				Constants.SESSION_USER);
		product.setUserId(user.getUserId());
		if(productPhoto!=null){
			upload();
		}
		product.setProductClick(0);
		product.setUserDate(new Date());
		productService.saveProduct(product);
		return listProduct();
	}
	/**
	 * 对产品对象分页列表
	 */
	public String listProduct(){
		flipData=productService.findAllProductInfo(flipData);
		return "list";
	}
	/**
	 * 查询产品对象，分页列表
	 */
	public String productSearch(){
		
		StringBuffer hql = new StringBuffer("select t.funcName,p.productName,p.productFlag,p.productTop," +
									"p.productClick,p.userDate,b.userName,p.productId from Product p,TitleFunc t,BaseUser b " +
									"where p.funcId = t.funcId and b.userId=p.userId ");
		if(!productName.equals("")&&!productName.isEmpty()){
			hql.append("and p.productName like '%"+productName+"%'");
		}
		if(!productIntro.equals("")&&!productIntro.isEmpty()){
			hql.append("and p.productIntro like '%"+productIntro+"%'");
		}
		if(!product.getFuncId().isEmpty()&& !product.getFuncId().equals("")){
			hql.append(" and p.funcId ='"+product.getFuncId()+"'");
		}
		hql.append("order by p.funcId,p.userDate desc");
		flipData = productService.findProductByInfo(hql.toString(),flipData);
		return "list";
	}
	/**
	 * 显示产品的具体内容
	 */
	public String productDetail(){
		
		product=productService.findProductById(productId);
		titleFunc=productService.findTitleFuncByFuncId(product.getFuncId());
		return "detail";
	}
	/**
	 * 删除产品对象
	 */
	@SuppressWarnings("deprecation")
	public String delete(){
		
		product=productService.findProductById(productId);
		String filePathAndName=request.getRealPath(File.separator)+Constants.PRODUCT_DIR+
			product.getProductPhoto();
		delFile(filePathAndName);
		productService.deleteProduct(product);
		return listProduct();
	}
	/**
	 * 获得要修改的对象
	 */
	public String modify(){
		
		product=productService.findProductById(productId);
		titleFunc=productService.findTitleFuncByFuncId(product.getFuncId());
		return "edit";
	}
	/**
	 * 保存修改后的产品对象
	 */
	public String modifyProduct(){
		
		if(productPhoto!=null){
			String filePathAndName=request.getRealPath(File.separator)+Constants.PRODUCT_DIR+
				product.getProductPhoto();
			delFile(filePathAndName);
			upload();
		}
		product.setUserDate(new Date());
		productService.updateProduct(product);
		return listProduct();
	}
	/**
	 * 上传文件
	 * @return
	 */
	public String upload(){
		
		String fileType = getProductPhotoFileName().substring(getProductPhotoFileName().lastIndexOf("."));
		String fileSourceName = getProductPhotoFileName().substring(0,getProductPhotoFileName().lastIndexOf("."));
		String rootDir = request.getRealPath(File.separator)+Constants.PRODUCT_DIR;
		String fileTimeName = String.valueOf(new Date().getTime());
		String fileDownDir = rootDir+fileTimeName+fileType;
		
		File rootFile = new File(rootDir);
		File fileDownFile = new File(fileDownDir);
		if (!rootFile.exists()){
			rootFile.mkdirs();
		}
		if(fileDownFile.exists()){
			fileDownFile.delete();
		}
		try{
			FileInputStream in = new FileInputStream(productPhoto);
			FileOutputStream out = new FileOutputStream(fileDownDir);
			byte[] buffer = new byte[1024];
			int i = 0;
			if(in!=null){
				while((i = in.read(buffer))> 0){
					out.write(buffer,0,i);
				}
			}
		}catch(FileNotFoundException e){
			e.printStackTrace();			
		}catch(IOException e){
			e.printStackTrace();
		}
		product.setProductPhoto(fileTimeName+fileType);
		return fileDownDir;
	}
	/**
	  * 删除在服务器上的文件
	  * @param filePathAndName
	  */
	public void delFile(String filePathAndName) { 
		
	     try { 
	       String filePath = filePathAndName; 
	       filePath = filePath.toString(); 
	       File myDelFile = new File(filePath); 
	       myDelFile.delete(); 
	     } 
	     catch (Exception e) { 
	       System.out.println("删除文件操作出错"); 
	       e.printStackTrace(); 
	     } 
	 } 
	/**
	 * @return the productService
	 */
	public ProductService getProductService() {
		return productService;
	}
	/**
	 * @param productService the productService to set
	 */
	public void setProductService(ProductService productService) {
		this.productService = productService;
	}
	/**
	 * @return the product
	 */
	public Product getProduct() {
		return product;
	}
	/**
	 * @param product the product to set
	 */
	public void setProduct(Product product) {
		this.product = product;
	}
	/**
	 * @return the productPhoto
	 */
	public String getProductPhoto() {
		return productPhoto;
	}
	/**
	 * @param productPhoto the productPhoto to set
	 */
	public void setProductPhoto(String productPhoto) {
		this.productPhoto = productPhoto;
	}
	/**
	 * @return the productPhotoFileName
	 */
	public String getProductPhotoFileName() {
		return productPhotoFileName;
	}
	/**
	 * @param productPhotoFileName the productPhotoFileName to set
	 */
	public void setProductPhotoFileName(String productPhotoFileName) {
		this.productPhotoFileName = productPhotoFileName;
	}
	/**
	 * @return the flipData
	 */
	public FlipPageInfo getFlipData() {
		return flipData;
	}
	/**
	 * @param flipData the flipData to set
	 */
	public void setFlipData(FlipPageInfo flipData) {
		this.flipData = flipData;
	}
	/**
	 * @return the productName
	 */
	public String getProductName() {
		return productName;
	}
	/**
	 * @param productName the productName to set
	 */
	public void setProductName(String productName) {
		this.productName = productName;
	}
	/**
	 * @return the productIntro
	 */
	public String getProductIntro() {
		return productIntro;
	}
	/**
	 * @param productIntro the productIntro to set
	 */
	public void setProductIntro(String productIntro) {
		this.productIntro = productIntro;
	}
	/**
	 * @return the productId
	 */
	public String getProductId() {
		return productId;
	}
	/**
	 * @param productId the productId to set
	 */
	public void setProductId(String productId) {
		this.productId = productId;
	}
	/**
	 * @return the titleFunc
	 */
	public TitleFunc getTitleFunc() {
		return titleFunc;
	}
	/**
	 * @param titleFunc the titleFunc to set
	 */
	public void setTitleFunc(TitleFunc titleFunc) {
		this.titleFunc = titleFunc;
	}

	
	
}